
package com.hlkj.pay.gateway.order.impl;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.framework.http.template.OkHttpTemplate;
import com.hlkj.pay.dto.merchant.MerchantAccessDto;
import com.hlkj.pay.dto.order.OrderFundDto;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.gateway.order.IOrderNotifyGateway;
import com.hlkj.pay.infrastructure.model.order.*;
import com.hlkj.pay.infrastructure.model.payment.MerchantPaymentDO;
import com.hlkj.pay.infrastructure.model.payment.MerchantPaymentDetailDO;
import com.hlkj.pay.vo.openapi.merchant.resp.access.OpenApiMerchantAccessResp;
import com.hlkj.pay.vo.openapi.merchant.resp.access.OpenApiOrderSettleResp;
import com.hlkj.pay.vo.openapi.order.resp.AcctRefundOrderApiResp;
import com.hlkj.pay.vo.openapi.order.resp.OpenApiOrderTransferResp;
import com.hlkj.pay.vo.openapi.order.resp.OpenApiOrderWithdrawResp;
import com.hlkj.pay.vo.openapi.order.resp.PaymentApiResp;
import com.hlkj.pay.vo.order.OpenApiResult;
import com.hlkj.pay.vo.order.resp.PayOrderResp;
import com.hlkj.pay.vo.order.resp.RefundOrderResp;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author HlpayTeam
 * @date 2024/11/07 11:04
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class OrderNotifyGatewayImpl implements IOrderNotifyGateway {

    @Value("${hlkj.barrier:false}")
    private boolean barrier;

    private final OkHttpTemplate okHttpTemplate;

    private final OrderNotifyParamsHandler orderNotifyParamsHandler;

    @Override
    public OrderNotifyDO notifyPayOrder(PayOrderDO payOrderDO, List<OrderFundDetailDO> payOrderFundList) {
        OrderNotifyDO orderNotifyDO = new OrderNotifyDO();
        orderNotifyDO.setNotifyUrl(payOrderDO.getNotifyUrl());
        if (barrier) {
            orderNotifyDO.setRequestParams(JsonUtils.toJsonString(payOrderDO));
            orderNotifyDO.setResponse(JsonUtils.toJsonString(CommonResult.success()));
            orderNotifyDO.setCostTime(10);
            orderNotifyDO.setNotifyTime(System.currentTimeMillis());
            orderNotifyDO.setNotifyState(CommonEnum.YES_NO_TYPE.YES.getCode());
            return orderNotifyDO;
        }
        OpenApiResult<PayOrderResp> openApiResult = orderNotifyParamsHandler.payOrderNotifyData(payOrderDO, payOrderFundList);
        String json = JsonUtils.toJsonString(openApiResult);
        orderNotifyDO.setRequestParams(json);
        try {
            Long start = System.currentTimeMillis();
            String post = okHttpTemplate.post(payOrderDO.getNotifyUrl(), json, null);
            orderNotifyDO.setResponse(post);
            long currentTimeMillis = System.currentTimeMillis() - start;
            orderNotifyDO.setCostTime((int) currentTimeMillis);
            if ("success".equals(post) || "SUCCESS".equals(post)) {
                orderNotifyDO.setNotifyState(CommonEnum.YES_NO_TYPE.YES.getCode());
            }
            else {
                orderNotifyDO.setNotifyState(CommonEnum.YES_NO_TYPE.NO.getCode());
            }
        }
        catch (Exception e) {
            orderNotifyDO.setResponse(e.getMessage());
            orderNotifyDO.setNotifyState(CommonEnum.YES_NO_TYPE.NO.getCode());
        }
        return orderNotifyDO;
    }

    @Override
    public OrderNotifyDO notifyRefundOrder(RefundOrderDO refundOrderDO) {
        OrderNotifyDO orderNotifyDO = new OrderNotifyDO();
        if (barrier) {
            orderNotifyDO.setRequestParams(JsonUtils.toJsonString(refundOrderDO));
            orderNotifyDO.setResponse(JsonUtils.toJsonString(CommonResult.success()));
            orderNotifyDO.setCostTime(10);
            orderNotifyDO.setNotifyUrl(refundOrderDO.getNotifyUrl());
            orderNotifyDO.setNotifyTime(System.currentTimeMillis());
            orderNotifyDO.setNotifyState(CommonEnum.YES_NO_TYPE.YES.getCode());
            return orderNotifyDO;
        }
        OpenApiResult<RefundOrderResp> openApiResult = orderNotifyParamsHandler.refundOrderNotifyData(refundOrderDO);
        String json = JsonUtils.toJsonString(openApiResult);
        orderNotifyDO.setRequestParams(json);
        try {
            Long start = System.currentTimeMillis();
            String post = okHttpTemplate.post(refundOrderDO.getNotifyUrl(), json, null);
            orderNotifyDO.setResponse(post);
            long currentTimeMillis = System.currentTimeMillis() - start;
            orderNotifyDO.setCostTime((int) currentTimeMillis);
            if ("success".equals(post) || "SUCCESS".equals(post)) {
                orderNotifyDO.setNotifyState(CommonEnum.YES_NO_TYPE.YES.getCode());
            }
            else {
                orderNotifyDO.setNotifyState(CommonEnum.YES_NO_TYPE.NO.getCode());
            }
        }
        catch (Exception e) {
            orderNotifyDO.setResponse(e.getMessage());
            orderNotifyDO.setNotifyState(CommonEnum.YES_NO_TYPE.NO.getCode());
        }
        return orderNotifyDO;
    }

    @Override
    public OrderNotifyDO notifyPaymentOrder(MerchantPaymentDO merchantPaymentDO, MerchantPaymentDetailDO merchantPaymentDetailDO) {
        OrderNotifyDO orderNotifyDO = new OrderNotifyDO();
        if (barrier) {
            orderNotifyDO.setRequestParams(JsonUtils.toJsonString(merchantPaymentDetailDO));
            orderNotifyDO.setResponse(JsonUtils.toJsonString(CommonResult.success()));
            orderNotifyDO.setCostTime(10);
            orderNotifyDO.setNotifyUrl(merchantPaymentDO.getNotifyUrl());
            orderNotifyDO.setNotifyTime(System.currentTimeMillis());
            orderNotifyDO.setNotifyState(CommonEnum.YES_NO_TYPE.YES.getCode());
            return orderNotifyDO;
        }
        OpenApiResult<PaymentApiResp> openApiResult = orderNotifyParamsHandler.paymentOrderNotifyData(merchantPaymentDO, merchantPaymentDetailDO);
        String json = JsonUtils.toJsonString(openApiResult);
        orderNotifyDO.setRequestParams(json);
        try {
            Long start = System.currentTimeMillis();
            log.info("最终回调通知参数:{} {}", merchantPaymentDetailDO.getDetailPaymentId(), json);
            String post = okHttpTemplate.post(merchantPaymentDO.getNotifyUrl(), json, null);
//            log.info("最终回调通知参数返回:{} [{}]", merchantPaymentDetailDO.getDetailPaymentId(), post);
            orderNotifyDO.setResponse(post);
            long currentTimeMillis = System.currentTimeMillis() - start;
            orderNotifyDO.setCostTime((int) currentTimeMillis);
            if ("success".equals(post) || "SUCCESS".equals(post)) {
                orderNotifyDO.setNotifyState(CommonEnum.YES_NO_TYPE.YES.getCode());
            }
            else {
                log.info("最终回调通知参数失败:{} {}", merchantPaymentDetailDO.getDetailPaymentId(), post);
                orderNotifyDO.setNotifyState(CommonEnum.YES_NO_TYPE.NO.getCode());
            }
        }
        catch (Exception e) {
            log.info("最终回调通知参数异常:{} {}", merchantPaymentDetailDO.getDetailPaymentId(), json, e);
            orderNotifyDO.setResponse(e.getMessage());
            orderNotifyDO.setNotifyState(CommonEnum.YES_NO_TYPE.NO.getCode());
        }
        return orderNotifyDO;
    }

    @Override
    public OrderNotifyDO notifyMerchantAccess(MerchantAccessDto merchantAccessDto) {
        String notifyUrl = merchantAccessDto.getMerchantAccessInfo().getNotifyUrl();
        OrderNotifyDO orderNotifyDO = new OrderNotifyDO();
        orderNotifyDO.setNotifyUrl(notifyUrl);
        OpenApiResult<OpenApiMerchantAccessResp> openApiResult = orderNotifyParamsHandler.merchantAccessNotifyData(merchantAccessDto);
        if (barrier) {
            orderNotifyDO.setRequestParams(JsonUtils.toJsonString(openApiResult.getData()));
            orderNotifyDO.setResponse(JsonUtils.toJsonString(CommonResult.success()));
            orderNotifyDO.setCostTime(10);
            orderNotifyDO.setNotifyTime(System.currentTimeMillis());
            orderNotifyDO.setNotifyState(CommonEnum.YES_NO_TYPE.YES.getCode());
            return orderNotifyDO;
        }
        String json = JsonUtils.toJsonString(openApiResult);
        orderNotifyDO.setRequestParams(json);
        try {
            Long start = System.currentTimeMillis();
            String post = okHttpTemplate.post(notifyUrl, json, null);
            orderNotifyDO.setResponse(post);
            long currentTimeMillis = System.currentTimeMillis() - start;
            orderNotifyDO.setCostTime((int) currentTimeMillis);
            if ("success".equals(post) || "SUCCESS".equals(post)) {
                orderNotifyDO.setNotifyState(CommonEnum.YES_NO_TYPE.YES.getCode());
            }
            else {
                orderNotifyDO.setNotifyState(CommonEnum.YES_NO_TYPE.NO.getCode());
            }
        }
        catch (Exception e) {
            orderNotifyDO.setResponse(e.getMessage());
            orderNotifyDO.setNotifyState(CommonEnum.YES_NO_TYPE.NO.getCode());
        }
        return orderNotifyDO;
    }

    @Override
    public OrderNotifyDO notifyMerchantAccessSettle(OrderSettleDO accessSettleOrderDO) {
        String notifyUrl = accessSettleOrderDO.getNotifyUrl();
        OrderNotifyDO orderNotifyDO = new OrderNotifyDO();
        orderNotifyDO.setNotifyUrl(notifyUrl);
        OpenApiResult<OpenApiOrderSettleResp> openApiResult = orderNotifyParamsHandler.merchantAccessSettleNotifyData(accessSettleOrderDO);
        if (barrier) {
            orderNotifyDO.setRequestParams(JsonUtils.toJsonString(openApiResult.getData()));
            orderNotifyDO.setResponse(JsonUtils.toJsonString(CommonResult.success()));
            orderNotifyDO.setCostTime(10);
            orderNotifyDO.setNotifyTime(System.currentTimeMillis());
            orderNotifyDO.setNotifyState(CommonEnum.YES_NO_TYPE.YES.getCode());
            return orderNotifyDO;
        }
        String json = JsonUtils.toJsonString(openApiResult);
        orderNotifyDO.setRequestParams(json);
        try {
            Long start = System.currentTimeMillis();
            String post = okHttpTemplate.post(notifyUrl, json, null);
            orderNotifyDO.setResponse(post);
            long currentTimeMillis = System.currentTimeMillis() - start;
            orderNotifyDO.setCostTime((int) currentTimeMillis);
            if ("success".equals(post) || "SUCCESS".equals(post)) {
                orderNotifyDO.setNotifyState(CommonEnum.YES_NO_TYPE.YES.getCode());
            }
            else {
                orderNotifyDO.setNotifyState(CommonEnum.YES_NO_TYPE.NO.getCode());
            }
        }
        catch (Exception e) {
            orderNotifyDO.setResponse(e.getMessage());
            orderNotifyDO.setNotifyState(CommonEnum.YES_NO_TYPE.NO.getCode());
        }
        return orderNotifyDO;
    }

    @Override
    public OrderNotifyDO notifyOrderFund(OrderFundDto orderFundDto) {
        OrderFundDO orderFund = orderFundDto.getOrderFund();
        String notifyUrl = orderFund.getNotifyUrl();
        OrderNotifyDO orderNotifyDO = new OrderNotifyDO();
        orderNotifyDO.setNotifyUrl(notifyUrl);
        OpenApiResult<AcctRefundOrderApiResp> openApiResult = orderNotifyParamsHandler.openApiMerchantAccessResp(orderFundDto);
        if (barrier) {
            orderNotifyDO.setRequestParams(JsonUtils.toJsonString(openApiResult.getData()));
            orderNotifyDO.setResponse(JsonUtils.toJsonString(CommonResult.success()));
            orderNotifyDO.setCostTime(10);
            orderNotifyDO.setNotifyTime(System.currentTimeMillis());
            orderNotifyDO.setNotifyState(CommonEnum.YES_NO_TYPE.YES.getCode());
            return orderNotifyDO;
        }
        String json = JsonUtils.toJsonString(openApiResult);
        orderNotifyDO.setRequestParams(json);
        try {
            Long start = System.currentTimeMillis();
            String post = okHttpTemplate.post(notifyUrl, json, null);
            orderNotifyDO.setResponse(post);
            long currentTimeMillis = System.currentTimeMillis() - start;
            orderNotifyDO.setCostTime((int) currentTimeMillis);
            if ("success".equals(post) || "SUCCESS".equals(post)) {
                orderNotifyDO.setNotifyState(CommonEnum.YES_NO_TYPE.YES.getCode());
            }
            else {
                orderNotifyDO.setNotifyState(CommonEnum.YES_NO_TYPE.NO.getCode());
            }
        }
        catch (Exception e) {
            orderNotifyDO.setResponse(e.getMessage());
            orderNotifyDO.setNotifyState(CommonEnum.YES_NO_TYPE.NO.getCode());
        }
        return orderNotifyDO;
    }

    @Override
    public OrderNotifyDO notifyOrderTransfer(OrderTransferDO orderTransferDO) {
        OrderNotifyDO orderNotifyDO = new OrderNotifyDO();
        orderNotifyDO.setNotifyUrl(orderTransferDO.getNotifyUrl());
        if (barrier) {
            orderNotifyDO.setRequestParams(JsonUtils.toJsonString(orderTransferDO));
            orderNotifyDO.setResponse(JsonUtils.toJsonString(CommonResult.success()));
            orderNotifyDO.setCostTime(10);
            orderNotifyDO.setNotifyTime(System.currentTimeMillis());
            orderNotifyDO.setNotifyState(CommonEnum.YES_NO_TYPE.YES.getCode());
            return orderNotifyDO;
        }
        OpenApiResult<OpenApiOrderTransferResp> openApiResult = orderNotifyParamsHandler.transferOrderNotifyData(orderTransferDO);
        String json = JsonUtils.toJsonString(openApiResult);
        orderNotifyDO.setRequestParams(json);
        try {
            Long start = System.currentTimeMillis();
            String post = okHttpTemplate.post(orderTransferDO.getNotifyUrl(), json, null);
            orderNotifyDO.setResponse(post);
            long currentTimeMillis = System.currentTimeMillis() - start;
            orderNotifyDO.setCostTime((int) currentTimeMillis);
            if ("success".equals(post) || "SUCCESS".equals(post)) {
                orderNotifyDO.setNotifyState(CommonEnum.YES_NO_TYPE.YES.getCode());
            }
            else {
                orderNotifyDO.setNotifyState(CommonEnum.YES_NO_TYPE.NO.getCode());
            }
        }
        catch (Exception e) {
            orderNotifyDO.setResponse(e.getMessage());
            orderNotifyDO.setNotifyState(CommonEnum.YES_NO_TYPE.NO.getCode());
        }
        return orderNotifyDO;
    }

    @Override
    public OrderNotifyDO notifyOrderWithdraw(OrderWithdrawDO orderWithdrawDO) {
        OrderNotifyDO orderNotifyDO = new OrderNotifyDO();
        orderNotifyDO.setNotifyUrl(orderWithdrawDO.getNotifyUrl());
        if (barrier) {
            orderNotifyDO.setRequestParams(JsonUtils.toJsonString(orderWithdrawDO));
            orderNotifyDO.setResponse(JsonUtils.toJsonString(CommonResult.success()));
            orderNotifyDO.setCostTime(10);
            orderNotifyDO.setNotifyTime(System.currentTimeMillis());
            orderNotifyDO.setNotifyState(CommonEnum.YES_NO_TYPE.YES.getCode());
            return orderNotifyDO;
        }
        OpenApiResult<OpenApiOrderWithdrawResp> openApiResult = orderNotifyParamsHandler.withdrawOrderNotifyData(orderWithdrawDO);
        String json = JsonUtils.toJsonString(openApiResult);
        orderNotifyDO.setRequestParams(json);
        try {
            Long start = System.currentTimeMillis();
            String post = okHttpTemplate.post(orderWithdrawDO.getNotifyUrl(), json, null);
            orderNotifyDO.setResponse(post);
            long currentTimeMillis = System.currentTimeMillis() - start;
            orderNotifyDO.setCostTime((int) currentTimeMillis);
            if ("success".equals(post) || "SUCCESS".equals(post)) {
                orderNotifyDO.setNotifyState(CommonEnum.YES_NO_TYPE.YES.getCode());
            }
            else {
                orderNotifyDO.setNotifyState(CommonEnum.YES_NO_TYPE.NO.getCode());
            }
        }
        catch (Exception e) {
            orderNotifyDO.setResponse(e.getMessage());
            orderNotifyDO.setNotifyState(CommonEnum.YES_NO_TYPE.NO.getCode());
        }
        return orderNotifyDO;
    }

}
